<template>
  <div style="height: 100vh;display: flex;align-items: center;justify-content: center;background-color: #669fef">
    <div style="display: flex;background-color: white;width: 50%;border-radius: 5px;overflow: hidden">
      <div style="flex: 1;">
        <img src="@/assets/css/register.png" style="width: 100%">
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <div style="font-size: 20px;font-weight: bold;text-align: center;margin-bottom: 20px">
            欢迎注册智能仓库管理系统
          </div>
          <el-form-item prop="no">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号"
                      v-model="user.no"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码"
                      v-model="user.confirmPass"></el-input>
          </el-form-item>
<!--          <el-form-item prop="role">-->
<!--            <el-input prefix-icon="el-icon-circle-check" size="medium" placeholder=""-->
<!--                      v-model="user.role"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item>
            <el-select size="medium" v-model="user.roleId" placeholder="请选择角色">
              <el-option label="超级管理员" value="0"></el-option>
              <el-option label="管理员" value="1"></el-option>
              <el-option label="普通用户" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="success" style="width: 100%" @click="register" >注 册</el-button>
          </el-form-item>
            <div style="flex: 1">已经有账号了? 请 <span style="color: #85ce61;cursor: pointer " @click="$router.push('/login1')">登录</span></div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>


export default {
  name: 'Register',
  data() {
    //校验确认密码
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'));
      }
      else if (confirmPass!== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      }
      else {
        callback();
      }
    }
    return {
      rules: {
        no: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        confirmPass: [
          {validator: validatePassword, trigger: 'blur'}
        ],
      },
      user: {
        confirmPass:'',
        roleId:'',
        no: '',
        password: ''
      }

    }
  },
  methods: {
    register() {
      this.$refs.registerRef.validate((valid) => {
        if (valid) {
          //验证通过
          this.$axios.post(this.$httpUrl + '/user/register', this.user).then(res => res.data).then(res => {
            console.log(res)
            if (res.code == 200) {
              //跳转到主页
              this.$router.push('/login1')
              this.$message.success("注册成功")
            } else {
              this.$message.error(res.msg)
              return false;
            }
          })
        } else {
          console.log('校验失败');
          return false;
        }
      })
    },
  }
}
</script>
<style scoped>

</style>